package eirkdbd.lab1.gui.tab;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import eirkdbd.lab1.log.MainTable;
import eirkdbd.lab1.log.model.FunctionsModel;
import eirkdbd.lab1.membership.functions.helpers.norms.implications.ImplicationFactory;
import eirkdbd.lab1.membership.functions.helpers.norms.snorms.SnormFactory;
import eirkdbd.lab1.membership.functions.helpers.norms.tnorms.TnormFactory;
import eirkdbd.lab1.obj.CBNorm;
import eirkdbd.lab1.obj.Function;

public abstract class AbstractConditionsTab extends TableTab<Function> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 9000L;
	
	private JComboBox<Function> cbFunctions;
	
	private JComboBox<CBNorm> cbNorms;
	
	private static Function[] functions = new Function[] {};
	
	private static CBNorm[] norm = new CBNorm[] {
		new CBNorm(SnormFactory.BOUNDEDSUM.getSnorm()),
		new CBNorm(SnormFactory.DRASTIC.getSnorm()),
		new CBNorm(SnormFactory.MAXIMUM.getSnorm()),
		new CBNorm(SnormFactory.PROBABILISTICSUM.getSnorm()),
		
		new CBNorm(TnormFactory.DRASTIC.getTnorm()),
		new CBNorm(TnormFactory.LUKASIEWICZ.getTnorm()),
		new CBNorm(TnormFactory.MINIMUM.getTnorm()),
		new CBNorm(TnormFactory.PRODUCT.getTnorm()),
		
		new CBNorm(ImplicationFactory.GODEL.getImplication()),
		new CBNorm(ImplicationFactory.GOGUEN.getImplication()),
		new CBNorm(ImplicationFactory.KLEENEDIENES.getImplication()),
		new CBNorm(ImplicationFactory.LUKASIEWICZ.getImplication())};
	
	protected void initSelectionPane() {
		JPanel pane = new JPanel();
		pane.setLayout(new GridLayout(2, 4, 10, 10));
		pane.setMinimumSize(new Dimension(800, 25));
		pane.setMaximumSize(new Dimension(800, 25));
		pane.setBackground(Color.darkGray);
		JLabel Function = new JLabel("Function:");
		Function.setForeground(Color.orange);
		pane.add(Function);
		cbFunctions = new JComboBox<Function>(functions);
		pane.add(cbFunctions);
		pane.add(new JLabel(""));
		pane.add(new JLabel(""));
		
		JLabel Function1 = new JLabel("Join Operator:");
		Function1.setForeground(Color.orange);
		pane.add(Function1);
		cbNorms = new JComboBox<CBNorm>(norm);
		pane.add(cbNorms);
		pane.add(new JLabel(""));
		pane.add(new JLabel(""));
	
		add(pane);
	}

	protected void initButtonPane() {
		JPanel pane = new JPanel();
		pane.setLayout(new FlowLayout());
		pane.setMaximumSize(new Dimension(800, 50));
		pane.setBackground(Color.darkGray);
		JButton bShow = new JButton("Add");
		bShow.addActionListener(new SaveListener());
		pane.add(bShow);
		
		JButton bClear = new JButton("Clear");
		bClear.addActionListener(new ClearListener());
		pane.add(bClear);
		
		add(pane);
	}

	protected void initTable() {
		table = new MainTable(new String[] {"Nr", "Function name", "Statistic", "Min value", "Max value", "Join operator"});
	}
	
	public void dataFromUi() {
	}
	
	
	public void refreshUi() {
		cbFunctions.removeAllItems();
		Iterator<Function> it = FunctionsModel.getInstance().getModelObjects();
		while(it.hasNext()) {
			cbFunctions.addItem(it.next());
		}
	}

	class SaveListener implements ActionListener {

		
		public void actionPerformed(ActionEvent e) {
			Function function = (Function) cbFunctions.getSelectedItem();
			function.setJoin((CBNorm)cbNorms.getSelectedItem());
			model.add(function);
			dataToUi();
		}
		
	}
	
	class ClearListener implements ActionListener {

		
		public void actionPerformed(ActionEvent e) {
			model.clear();
			dataToUi();
		}
		
	}
}